<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/25
 * Time: 9:42
 */
namespace app\api\controller;
use think\Db;
use think\Request;
class Index extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    /***
     * @return \think\response\Json
     * 联想词
     */
    public function lenovo(){
        $keyword = input('param.keyword');
        $res =array();
        if($keyword==''){
            return json(['status' => 200, 'msg' =>$res]);
        }else{
            $where['lenovo_name'] = array('like',"%$keyword%");
            $where['lenovo_status'] =1;
            $res = Db::table('up_lenovo')->where($where)->select();
            return json(['status' => 200, 'msg' =>$res]);
        }
    }

    /***
     * @return \think\response\Json
     * 热门搜索
     */
    public function hot(){
        $where['hot_status'] =1;
        $res = Db::table('up_hot')->where($where)->select();
        return json(['status' => 200, 'msg' =>$res]);
    }


    /***
     * @return \think\response\Json
     * 搜索
     */
    public function keyword(){
        extract(input());
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $res = array();
        if($keyword==''){
            $res['wy'] = array();
            $res['topic'] = array();
            return json(['status' => 200, 'msg' =>$res]);
        }else{
            $where2['xq_sh_status'] = 2;
            $where2['xq_user_id'] = $user_id;
            $result = Db::table('up_xq_property')->distinct(true)->where($where2)->column('xq_id');
            //查询物业通知
            $where['wy_title'] = array('like',"%$keyword%");
            $where['wy_xq_id'] = array('IN',$result);
            $info =Db::table('up_wy_notice')->alias('a')
                ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
                ->where($where)->field('a.wy_id,a.wy_title,a.wy_time,ux.xq_name')
                ->limit($limit,$num)->select();
            //查询话题
            $topic_id_list = Db::table('up_topic_qx')->where('user_id',$user_id)->column('topic_id');
            $map['topic_id'] = array('not in',$topic_id_list);
            $map['topic_title'] = array('like',"%$keyword%");
            $map['topic_is_show'] = 1;
            $info1 =Db::table('up_topic')->where($map)
                ->field('topic_user_id,topic_id,topic_title,topic_content,topic_img')
                ->limit($limit,$num)->select();
            foreach ($info1 as $key=>&$vo){
                $dz_num = Db::table('up_topic_dz')->where('topic_id',$vo['topic_id'])->count();
                $whereMap['topic_id'] = $vo['topic_id'];
                $whereMap['topic_status'] = 1;
                $message_num = Db::table('up_topic_message')->where($whereMap)->count();
                $like_num = Db::table('up_topic_like')->where('topic_id',$vo['topic_id'])->count();
                $vo['like_num'] = $like_num;
                $vo['dz_num'] = $dz_num;
                $vo['message_num'] = $message_num;

                //判断这个用户是否点赞
                $where1['topic_dz_user'] = $user_id;
                $where1['topic_id'] = $vo['topic_id'];
                $info2 = Db::table('up_topic_dz')->where($where1)->find();
                if($info2['topic_dz_id']!=''){//证明点赞了
                    $vo['dz'] = 1;
                }else{
                    $vo['dz'] = 2;
                }
                //判断这个用户是否收藏
                $where3['topic_like_user'] = $user_id;
                $where3['topic_id'] = $vo['topic_id'];
                $info3 = Db::table('up_topic_like')->where($where3)->find();
                if($info3['topic_like_id']!=''){//证明收藏了
                    $vo['like'] = 1;
                }else{
                    $vo['like'] = 2;
                }
            }
            $res['wy'] = $info;
            $res['topic'] = $info1;
            return json(['status' => 200, 'msg' =>$res]);
        }
    }

    /***
     * @return \think\response\Json
     * banner图
     */
    public function banner(){
        $res = Db::table('up_banner')->where('banner_status',1)->order('banner_sort asc')->select();
        foreach ($res as $key=>&$vo){
            if($vo['banner_type']==3){//话题
               $topic_user_id=  Db::table('up_topic')->where('topic_id',$vo['banner_object_id'])->value('topic_user_id');
                $vo['topic_user_id'] =$topic_user_id;
            }else{
                $vo['topic_user_id'] ='';
            }
        }
        return json(['status' => 200, 'msg' =>$res]);
    }

    /***
     * @return \think\response\Json
     * 首页图标
     */
    public function icon(){
        $where['icon_status'] = 1;
        $where['icon_id'] = array('<',9);
        $res = Db::table('up_icon')->field('icon_id,icon_name,icon_thumb')->where($where)->order('icon_sort asc')->select();
        return json(['status' => 200, 'msg' =>$res]);
    }
    /***
     * @return \think\response\Json
     * 首页图标
     */
    public function iconS(){
        $id = input('param.icon_id');
        $where['icon_status'] = 1;
        $where['icon_pid'] =$id;
        $res = Db::table('up_icon')->field('icon_id,icon_name,icon_thumb')->where($where)->order('icon_sort asc')->select();
        return json(['status' => 200, 'msg' =>$res]);
    }

    /***
     * @return \think\response\Json
     *
     */
    public function iconDetail(){
        $id = input('param.icon_id');
        $res = Db::table('up_icon')->where('icon_id',$id)->find();
        return json(['status' => 200, 'msg' =>$res]);
    }

    /***
     * @return \think\response\Json
     * 预约看房
     */
    public function yuyue(){
        extract(input());
        $data['yy_user_id'] = $user_id;
        $data['yy_username'] = input('param.name');
        $data['yy_phone'] = input('param.phone');
        $data['yy_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_yuyue')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' =>'预约成功']);
        }else{
            return json(['status' => 200, 'msg' =>"预约失败"]);
        }
    }
    /***
     * @return \think\response\Json
     * 物业通知
     */
    public function property(){
        extract(input());
        $where['xq_sh_status'] = 2;
        $where['xq_user_id'] = $user_id;
        $info = Db::table('up_xq_property')->distinct(true)->where($where)->column('xq_id');
        if(count($info)>0){//证明自己已经认证的小区已经通过
            $map['wy_xq_id'] = array('IN',$info);
            $res =Db::table('up_wy_notice')->alias('a')
                ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
                ->where($map)->field('a.wy_id,a.wy_title,a.wy_time,ux.xq_name')
                ->order('a.wy_time desc')
                ->limit(3)->select();
        }else{
            $res =Db::table('up_wy_notice')->alias('a')
                ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
                ->field('a.wy_id,a.wy_title,a.wy_time,ux.xq_name')
                ->order('a.wy_time desc')
                ->limit(3)->select();

        }
        return json(['status' => 200, 'msg' =>$res]);
    }

    //话题
    public function topic(){
        extract(input());
        $topic_id_list = Db::table('up_topic_qx')->where('user_id',$user_id)->column('topic_id');
        //查询话题
        $map['topic_is_show'] = 1;
        $map['topic_id'] = array('not in',$topic_id_list);
        $info1 =Db::table('up_topic')->where($map)
            ->field('topic_user_id,topic_id,topic_title,topic_content,topic_img')
            ->order('topic_num desc,topic_fb_time desc')
            ->limit(15)->select();
        foreach ($info1 as $key=>&$vo){
            //点赞
            $dz_num = Db::table('up_topic_dz')->where('topic_id',$vo['topic_id'])->count();
            //留言
            $whereMap['topic_id'] = $vo['topic_id'];
            $whereMap['topic_status'] = 1;
            $message_num = Db::table('up_topic_message')->where($whereMap)->count();
            //收藏
            $like_num = Db::table('up_topic_like')->where('topic_id',$vo['topic_id'])->count();
            $vo['dz_num'] = $dz_num;
            $vo['message_num'] = $message_num;
            $vo['like_num'] = $like_num;

            //判断这个用户是否收藏
            $where['topic_like_user'] = $user_id;
            $where['topic_id'] = $vo['topic_id'];
            $info3 = Db::table('up_topic_like')->where($where)->find();
            if($info3['topic_like_id']!=''){//证明收藏了
                $vo['like'] = 1;
            }else{
                $vo['like'] = 2;
            }
            //判断这个用户是否点赞
            $where1['topic_dz_user'] = $user_id;
            $where1['topic_id'] = $vo['topic_id'];
            $info2 = Db::table('up_topic_dz')->where($where1)->find();
            if($info2['topic_dz_id']!=''){//证明点赞了
                $vo['dz'] = 1;
            }else{
                $vo['dz'] = 2;
            }
        }
        return json(['status' => 200, 'msg' =>$info1]);
    }
}